<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改班级')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-room-edit" th:object="${sysRoom}">
            <input name="roomId" th:field="*{roomId}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">班级名称：</label>
                    <div class="col-sm-8">
                        <input name="roomName" th:field="*{roomName}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>

            <div class="col-xs-12">
                <div id="element" class="form-group">
                    <label class="col-sm-3 control-label is-required">分校/班主任：</label>
                    <div class="col-sm-4">
                        <select class="dept form-control m-b" name="deptId" th:field="*{deptId}" data-first-title="请选择">
                            <option value="">请选择</option>
                        </select>
                    </div>
                    <div class="col-sm-4">
                        <select class="user form-control m-b" name="userId" th:field="*{userId}" data-first-title="请选择">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
            </div>

            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">状态：</label>
                    <div class="col-sm-8">
                        <div class="radio-box" th:each="dict : ${@dict.getType('sys_normal_disable')}">
                            <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{status}" required>
                            <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">备注：</label>
                    <div class="col-sm-8">
                        <textarea name="remark" class="form-control">[[*{remark}]]</textarea>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: jquery-cxselect-js" />
    <script th:inline="javascript">
        var userDatas = [[${@common.getMasterSelectJson()}]];
        $('#element').cxSelect({
            selects: ['dept','user'],
            jsonValue: 'v',
            data: userDatas
        });

        var prefix = ctx + "system/room";
        $("#form-room-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit", $('#form-room-edit').serialize());
            }
        }
        // 设置默认值
        $(document).ready(function() {
            var deptId = [[${sysRoom.deptId}]];
            var userId = [[${sysRoom.userId}]];

            if (deptId) {
                // 设置 deptId 的默认值
                $('select[name="deptId"]').val(deptId);

                // 手动触发 cxSelect 的联动逻辑
                $('select[name="deptId"]').trigger('change');

                // 等待 cxSelect 加载完 userId 的选项后，再设置 userId 的默认值
                setTimeout(function() {
                    if (userId) {
                        $('select[name="userId"]').val(userId);
                    }
                }, 100); // 100ms 延迟，确保选项加载完成
            }
        });
    </script>
</body>
</html>